#include <iostream>

#include "city.hpp"
#include "foreach.hpp"
#include "merchant.hpp"
#include "occupation.hpp"
#include "resource.hpp"
#include "tile.hpp"

namespace {
std::vector<const occupation*> all_occupations;
}

const std::vector<const occupation*>& occupation::all()
{
	static bool first_time = true;
	if(first_time) {
		first_time = false;
		all_occupations.push_back(merchant_occupation::instance());
	}

	return all_occupations;
}

occupation::~occupation()
{
}

const merchant_occupation* merchant_occupation::instance()
{
	static const merchant_occupation m;
	return &m;
}

int merchant_occupation::earnings_estimate(const world& w, const_city_ptr ct, const citizen& cz) const
{
	return estimate_merchant_profits(w, ct, 500);
}

void craftsman_occupation::init()
{
	const std::vector<const_resource_ptr>& global_resources = resource::get_global_resources();
	foreach(const_resource_ptr r, global_resources) {
		all_occupations.push_back(new craftsman_occupation(r));
	}
}

craftsman_occupation::craftsman_occupation(const_resource_ptr r)
  : description_(r->specialization()), resource_(r)
{
}

int craftsman_occupation::earnings_estimate(const world& w, const_city_ptr ct, const citizen& cz) const
{
	assert(resource_->produces().empty() == false);
	const_item_type_ptr item = item_type::get(resource_->produces());
	assert(item);
	const int revenues = ct->market().can_sell_at(item);

	const int result = revenues*4*resource_->production_rate() - capital_required(w, ct);
	std::cerr << "earnings estimate for " << description() << ": " << result << " based on revenue of " << revenues << "\n";
	return result;
}

int craftsman_occupation::capital_required(const world& w, const_city_ptr ct) const
{
	int expenses = 0;
	if(resource_->uses().empty() == false) {
		const_item_type_ptr item = item_type::get(resource_->uses());
		assert(item);
		expenses = ct->market().can_buy_at(item);
	}
	
	return expenses*4*resource_->production_rate();
}

const char* craftsman_occupation::description() const
{
	return description_.c_str();
}

bool craftsman_occupation::do_work(world& w, hex::tile& t, citizen& c) const
{
	money_market& market = t.city()->market();
	int nproduce = 4*resource_->production_rate();
	if(c.items_owned(resource_->produces()) > nproduce) {
		//we already have too many of these items; quit our job.
		return false;
	}
	int price = 0;
	if(resource_->uses().empty() == false) {
		const_item_type_ptr input = item_type::get(resource_->uses());
		assert(input);
		int nbuy = 0;
		price = market.can_buy_at(input);
		price += market.tax_on_sale(price);
		while(c.items_owned("silver") >= price && nbuy < nproduce) {
			c.get_item("silver", -price);
			c.get_item(resource_->uses(), 1);
			t.city()->owner()->get_money(market.tax_on_sale(price));
			market.buy_item(input, price);
			++nbuy;
			price = market.can_buy_at(input);
			price += market.tax_on_sale(price);
		}

		nproduce = nbuy;
	}

	if(resource_->uses().empty() == false) {
		c.get_item(resource_->uses(), -nproduce);
	}

	c.get_item(resource_->produces(), nproduce);

	money_market::offer sell;
	sell.type = item_type::get(resource_->produces());
	sell.quantity = nproduce;
	sell.price = price+1;
	sell.trader = &c;
	return true;
}
